package com.gao.sqlserverspeedy.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidPooledConnection;
import com.gao.sqlserverspeedy.entity.SqlBase;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.sql.SQLException;

/**
 * @ClassName BaseServiceImpl
 * @Description
 * @Author Gaoyangzhou
 * @Date 2023-09-16 9:33
 * @Version 1.0
 */
@Service
public class BaseServiceImpl implements BaseService {
    @Value("${spring.datasource.druid.url}")
    private String url;
    @Value("${spring.datasource.druid.username}")
    private String username;
    @Value("${spring.datasource.druid.password}")
    private String password;
    @Value("${spring.datasource.druid.driverClassName}")
    private String driverClassName;

    public static SqlBase sqlBase;
    @Override
    public SqlBase changeDataSource(SqlBase sqlBase) {
        DruidDataSource druidDataSource = new DruidDataSource();
        //druidDataSource.setUrl("jdbc:sqlserver://"+ip+";databaseName="+dataBaseName+";useAffectedRows=true;trustServerCertificate=true;");
        druidDataSource.setUrl(sqlBase.getUrl());
        druidDataSource.setUsername(sqlBase.getUsername());
        druidDataSource.setPassword(sqlBase.getPassword());
        druidDataSource.setDriverClassName(this.driverClassName);
        druidDataSource.setRemoveAbandoned(false);//true是否自动回收超时连接
        // druidDataSource.setRemoveAbandonedTimeout(600);//超时时间(以秒数为单位)
        druidDataSource.setLogAbandoned(true);//是否在自动回收超时连接的时候打印连接的超时错误
        druidDataSource.setBreakAfterAcquireFailure(true);//true关闭重连
        druidDataSource.setTimeBetweenConnectErrorMillis(60);//设置多久重试
        druidDataSource.setConnectionErrorRetryAttempts(0);//重连次数，关闭重连需设为0
        druidDataSource.setMaxWait(3000);// 超时等待时间以毫秒为单位
        DynamicDataSource.dataSourcesMap.put("defaultDataSource", druidDataSource);
        DynamicDataSource.setDataSource("defaultDataSource");//切换数据源
        try {
            DruidPooledConnection dpc = druidDataSource.getConnection();//关键！尝试连接，否则无法捕获druid报错
            if(dpc!=null){
                sqlBase = sqlBase;
                return sqlBase;
            }
        } catch (SQLException e) {

            this.resetDefaultDataBase();//连接失败后，重置默认数据源
        }
        return new SqlBase();
    }
    @Override
    public Boolean resetDefaultDataBase() {
        DruidDataSource druidDataSource = new DruidDataSource();
        druidDataSource.setUrl(this.url);
        druidDataSource.setUsername(this.username);
        druidDataSource.setPassword(this.password);
        druidDataSource.setDriverClassName(this.driverClassName);
        druidDataSource.setRemoveAbandoned(false);
        druidDataSource.setLogAbandoned(true);
        druidDataSource.setBreakAfterAcquireFailure(true);
        druidDataSource.setTimeBetweenConnectErrorMillis(60);
        druidDataSource.setConnectionErrorRetryAttempts(0);
        druidDataSource.setMaxWait(3000);
        DynamicDataSource.dataSourcesMap.put("defaultDataSource", druidDataSource);
        DynamicDataSource.setDataSource("defaultDataSource");//切换数据源
        try {
            DruidPooledConnection dpc = druidDataSource.getConnection();
            if(dpc!=null){
                return true;
            }
        } catch (SQLException e) {

        }
        return false;
    }

    @Override
    public SqlBase getSQLBaes() {
        SqlBase sqlBase = new SqlBase();
        sqlBase.setUrl(url);
        sqlBase.setUsername(username);
        return sqlBase;
    }

}
